package com.example.demo.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;


public class ImageUtil {
	
	/**长度超过该值需压缩*/
	public static final int COMPRESS_LIMIT=1280; 
	/**图片最大kb*/
	public static final int SIZE_LIMIT=200;
	
	public static final String IMAGE_FORMAT="jpg";
	
	/**
	 * 压缩处理图片
	 * @return
	 * @throws IOException 
	 */
	public static byte[] handleImage(InputStream input) throws IOException{
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len;
		while ((len = input.read(buffer)) > -1 ) {
			baos.write(buffer, 0, len);
		}
		baos.flush();	
		
		//获取尺寸压缩比例
		double rate=getCompressRate(new ByteArrayInputStream(baos.toByteArray()));
		//System.out.println("压缩比例为:"+rate);
		//获取尺寸压缩后图片大小
		ByteArrayOutputStream out=compress(new ByteArrayInputStream(baos.toByteArray()), rate, 1.0D);
		
		byte[] result=out.toByteArray();
		
		int leng=result.length/1024;//图片大小写kb;
		//System.out.println("图片大小:"+leng);
		//获取尺寸压缩后图片大小 end
		
		//压缩质量
		BufferedImage img =null;
		
		double quality=1.0d;
         
	    while(leng>=SIZE_LIMIT){
	    	quality=sub(quality,0.1d);//质量递减
	    	out=compress(new ByteArrayInputStream(result), 1.0D, quality);
	    	result=out.toByteArray();
	    	img =ImageIO.read(new ByteArrayInputStream(result));
	    	leng=result.length/1024;
	    	out.reset();
	    }
	    //压缩质量 end
	    //System.out.println("最终图片大小:"+leng);
	    //System.out.println("最终图片压缩质量:"+quality);
	    out.close();
	    baos.close();
	    return result;
		
		
	}
	
	public static ByteArrayOutputStream compress(InputStream input,double rate,double quality) throws IOException{
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//压缩
		Thumbnails.of(input)
			.scale(rate)
			.outputQuality(quality)  
	        .outputFormat(IMAGE_FORMAT) 
	        .toOutputStream(baos); 
		return baos;
		//return new ByteArrayInputStream(baos.toByteArray());
	}
	
	
	public static Double sub(Double v1,Double v2){
        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());
        return b1.subtract(b2).doubleValue();
    }
	
	
	/**
	 * 获取压缩比例  大于1280的 取最长边于1280相比
	 * @param input
	 * @return
	 * @throws IOException
	 */
	public static double getCompressRate(InputStream input) throws IOException{
		
		BufferedImage img = ImageIO.read(input);
		int imgWidth = img.getWidth(null);
		int imgHeight = img.getHeight(null);
		
		double rate=1.0;  //缩放比例
		
		if(imgWidth>COMPRESS_LIMIT&&imgHeight>COMPRESS_LIMIT){
			
			if(imgWidth>imgHeight){
				rate=COMPRESS_LIMIT*rate/imgWidth;
			}else{
				rate=COMPRESS_LIMIT*rate/imgHeight;
			}
		}else if(imgWidth>COMPRESS_LIMIT&&imgHeight<=COMPRESS_LIMIT){
			rate=COMPRESS_LIMIT*rate/imgWidth;
		}else if(imgWidth<=COMPRESS_LIMIT&&imgHeight>COMPRESS_LIMIT){
			rate=COMPRESS_LIMIT*rate/imgHeight;
		}
		
		return rate;
		
	}
}
